#ifndef CHATDIALOG_H
#define CHATDIALOG_H

#include "ui_chatdialog.h"
#include "client.h"
#include "typeframe.h"
#include "handwritingframe.h"

#include <QState>
#include <QTextEdit>

class ChatDialog : public QDialog, private Ui::ChatDialog
{
    Q_OBJECT

public:
    ChatDialog(QWidget *parent = 0);

protected:
    void resizeEvent(QResizeEvent *event);

private slots:
    void appendMessage(Connection::DataScope scope, const QString &from, const QVariant &message);
    void sendMessage(const QVariant &mesg);
    void newParticipant(const QString &nick, const QHostAddress &addr);
    void participantLeft(const QString &nick);
    void showLogData();
    void newTab(QListWidgetItem *item);
    QTextEdit *newTab(const QString &title, bool active);

private:
    inline void initInputFrame();
    inline void initStateMachine();
    inline void initDataLogger();
    inline void initializeMember();
    inline void fillBlackList();
    inline void connectSignalsAndSlots();
    inline QTextEdit *currentTextEdit() const;
    inline QTextEdit *textEditFromTitle(const QString &title);
    inline QHostAddress currentAddress() const;
    void appendMessage(const QTextEdit &currTextEdit, const QString &from, const QVariant &message) const;
    void writeLog(Connection::DataScope scope, const QString &from, const QString &to, const QVariant &data);
    void initStates();
    void updateStates();

private:
    Client client;
    QString myNickName;
    QString publicChatLable;
    QSet<QString> addrBlackList;
    QTextTableFormat tableFormat;
    QTextEdit *textEdit;
    QPushButton *cornerButton;
    TypeFrame *typeFrame;
    HandWritingFrame *handWritingFrame;
    QStateMachine *stateMachine;
    QState *typeState;
    QState *handWritingState;
};

#endif
